//
//  ClockView.m
//  05-手势解锁
//
//  Created by Gavin on 15/9/22.
//  Copyright (c) 2015年 Gavin. All rights reserved.
//

#import "ClockView.h"

@interface ClockView()

/**
 *  选中的按钮数组.
 */
@property(nonatomic,strong)NSMutableArray *selectBtn;

/**
 *  当前手指移动的点
 */
@property(nonatomic,assign)CGPoint curP;

@end



@implementation ClockView

//懒加载数组.
-(NSMutableArray *)selectBtn{
    
    if (_selectBtn == nil) {
        _selectBtn = [NSMutableArray array];
    }
    return _selectBtn;
}

-(void)awakeFromNib{
    //初始化
    [self setUP];
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
         //初始化
        [self setUP];
    }
    return self;
}

//初始化
- (void)setUP{
    
    for (int i = 0; i < 9;  i++) {
        //添加按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //添加按钮时设置一个Tag以便记录每一个选中的按钮
        btn.tag = i;
        
        //让按钮不能够接受事件,原因是当前按钮会拦截事件.
        btn.userInteractionEnabled = NO;
        
        //设置图片
        [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        
        //设置选中状态的下图片
        [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        [self addSubview:btn];
    }
    
}

/**
 *  获取当前手指所在的点
 *
 *  @param touches touches集合
 *
 *  @return 当前手指所在的点.
 */
- (CGPoint)getCurrentPoint:(NSSet *)touches{
    
    UITouch *touch = [touches anyObject];
    return [touch locationInView:self];
}


/**
 *  判断一个点在不在按钮上.
 *
 *  @param point 当前点
 *
 *  @return 如果在按钮上, 返回当前按钮, 如果不在返回nil.
 */
- (UIButton *)btnRectContainsPoint:(CGPoint)point{

    for (UIButton *btn in self.subviews) {
        
        if (CGRectContainsPoint(btn.frame, point)) {
            //在按钮上.返回当前按钮
            return btn;
        }
    }
    return nil;
}


//手指点击时让按钮成选中状态
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    //判断当前手指在不在按钮上,如果在按钮上, 让按钮成为选中状态.
    //1.获取当前手指所在的点
    CGPoint curP = [self getCurrentPoint:touches];
    
    //2.判断当前手指所在的点在不在按钮上.
   UIButton *btn  = [self btnRectContainsPoint:curP];
    
    if (btn && btn.selected == NO) {//如果按钮已经是选中状态,就不让它再添加到数组当中
        //让按钮成为选中状态
        btn.selected = YES;
        //把选中按钮添加到数组当中
        [self.selectBtn addObject:btn];
        
    }
    
}

//手指移动时,按钮选中,连线到当前选中的按钮
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    //判断当前手指在不在按钮上,如果在按钮上, 让按钮成为选中状态.
    //1.获取当前手指所在的点
    CGPoint curP = [self getCurrentPoint:touches];
    //2.判断当前手指所在的点在不在按钮上.
    UIButton *btn  = [self btnRectContainsPoint:curP];
    if (btn && btn.selected == NO) {//如果按钮已经是选中状态,就不让它再添加到数组当中
        //让按钮成为选中状态
        btn.selected = YES;
        //把选中按钮添加到数组当中
        [self.selectBtn addObject:btn];
    }
    //每次做一次重绘.
    [self setNeedsDisplay];
    //记录当前手指移动的点.
    self.curP = curP;
}

//手指松开时,按钮取消选中状态,清空所有的连线.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{


    if (self.selectBtn.count) {
        
        //1.取消所有选中的按钮,查看选中按钮的顺序
        NSMutableString *str = [NSMutableString string];
        for (UIButton *btn in self.selectBtn) {
            [str appendFormat:@"%ld",btn.tag];
            btn.selected = NO;
        }
        //2.清空所有的连线.
        [self.selectBtn removeAllObjects];
        
        //3.重绘
        [self setNeedsDisplay];
        
        //查看是否是第一次设置密码
        NSString *keyPwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"keyPwd"];
        if (!keyPwd) {
         [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"keyPwd"];
         [[NSUserDefaults standardUserDefaults] synchronize];
             NSLog(@"第一次输入密码");
        }else{
            
            if ([keyPwd isEqualToString:str]) {
                NSLog(@"密码正确");
                UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"手势输入正确" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertV show];
                
                
            }else{
                NSLog(@"密码错误");
                  UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"手势输入错误" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertV show];
            }
        }
        NSLog(@"选中按钮顺序为:%@",str);
    }
}

//布局子控件
- (void)layoutSubviews{
    [super layoutSubviews];
    //总列数
    int cloumn = 3;
    CGFloat btnWH = 74;
    //每列之间的间距
    CGFloat margin = (self.bounds.size.width - cloumn * btnWH) / (cloumn + 1);
    
    //当前所在的列
    int curClounm = 0;
    //当前所在的行
    int curRow = 0;
    
    CGFloat x = 0;
    CGFloat y = 0;
    
    //取出所有的控件
    for (int i = 0; i < self.subviews.count; i++) {
        curClounm = i % cloumn;
        curRow = i / cloumn;
        x = margin + (margin + btnWH) * curClounm;
        y = (margin +btnWH) * curRow;
        UIButton *btn = self.subviews[i];
        //设置按钮的尺寸位置
        btn.frame = CGRectMake(x, y, btnWH, btnWH);
    }
}

- (void)drawRect:(CGRect)rect {
    //如果数组当中没有元素,就不让它进行绘图.直接返回.
    if(self.selectBtn.count <= 0) return;
    //创建路径.
    UIBezierPath *path = [UIBezierPath bezierPath];
    //取出所有保存的选中按钮连线.
    for(int i = 0; i < self.selectBtn.count;i++){
        UIButton *btn = self.selectBtn[i];
        //判断当前按钮是不是第一个,如果是第一个,把它的中心设置为路径的起点.
        if(i == 0){
            //设置起点.
            [path moveToPoint:btn.center];
        }else{
            //添加一根线到当前按钮的圆心.
            [path addLineToPoint:btn.center];
        }
    }
    //连完先中的按钮后, 在选中按钮之后,添加一根线到当前手指所在的点.
    [path addLineToPoint:self.curP];
    //设置颜色
    [[UIColor redColor] set];
    //设置线宽
    [path setLineWidth:10];
    //设置线的连接样式
    [path setLineJoinStyle:kCGLineJoinRound];
    //绘制路径.
    [path stroke];
}

@end
